package com.tuling.jucdemo.atomic;
/**
 * 原子更新引用类型
 */

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.concurrent.atomic.AtomicReference;


public class AtomicReferenceTest {
    public static void main(String[] args) {
        User user1 = new User("张三",23);
        User user2 = new User("李四",25);
        User user3 = new User("王五",27);

        //初始化为user1
        AtomicReference<User> atomicReference = new AtomicReference();
        atomicReference.set(user1);

        //把user2的值赋值给atomicReference
        atomicReference.compareAndSet(user1, user2);
        System.out.println(atomicReference.get());

        //把user3的值赋值给atomicReference
        atomicReference.compareAndSet(user1, user3);
        System.out.println(atomicReference.get());


    }
}

@Data
@AllArgsConstructor
class User{
    private String name;
    private int age;
}
